use prometheus::{Encoder, IntCounter, Registry,HistogramVec, register_histogram_vec};
use lazy_static::lazy_static;
use std::time::{Duration};
use std::thread::sleep;
lazy_static! {
    pub static ref DEFULAT_COUNTER:IntCounter = IntCounter::new("default", "generic number").unwrap();
    pub static ref ACTION:IntCounter = IntCounter::new("action", "action number").unwrap();
    pub static ref HTTP_REQ_HISTOGRAM: HistogramVec = register_histogram_vec!(
        "example_http_request_duration_seconds",
        "The HTTP request latencies in seconds.",
        &["handler"]
    )
    .unwrap();
}
pub fn register_metrics(registry:&Registry) {
    registry.register(Box::new(DEFULAT_COUNTER.clone())).unwrap();
    registry.register(Box::new(ACTION.clone())).unwrap();
    let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["all"]).start_timer();
    sleep(Duration::from_secs(1));
    timer.observe_duration();
    HTTP_REQ_HISTOGRAM.with_label_values(&["utt"]).observe(10.0);

}

pub async fn metric_handler() -> String {
    let mut buffer = Vec::new();
    let encoder = prometheus::TextEncoder::new();
    encoder.encode(&prometheus::gather(), &mut buffer).unwrap();
    String::from_utf8(buffer).unwrap()
}